package com.csg.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.csg.entity.User;

import com.csg.service.UserService;


@Controller
public class UserController {
	
	//注入service
	@Autowired
	private UserService userService;
	@RequestMapping("/selectUsers")
	public String selectAllUsers(String username,String password,Model model,HttpServletRequest request){
		
		User user = this.userService.selectUsers(username,password);
		if(user !=null){
			request.getSession().setAttribute("user", user);
			model.addAttribute("user", user);
			return "forward:shouye";
//			return "/WEB-INF/main.jsp";
		}
		return "/login.jsp";
	}
	//使用restful风格
	@RequestMapping("/{page}")
	public String ssm(@PathVariable("page") String page){
		return "/WEB-INF/"+page+".jsp";
	}
	
	@RequestMapping(value = "registerUser", method = RequestMethod.POST)
	public String registerUser(String username,String password,String email) {
		int findUser = userService.findUser(email);
 
		if (findUser == 0) {
			userService.regist(username, password, email);
			// System.out.println("可以注册");
			return "/login.jsp";
		} else {
			// System.out.println("注册失败");
			return "/register.jsp";
		}
	}
	
	
	
	//跳转到本工程主页的action
//		@RequestMapping(value = "/index")
//		public String index() {
//			return "index";
//		}
	 
		//关于用户登录的表单提交
	/*	@RequestMapping(value = "login", method = RequestMethod.POST)
		public String login(@RequestParam("username") String username,
				@RequestParam("password") String password,ModelMap model) {
			if (!userService.isUserExist(username)) {
				model.addAttribute("msg", "用户名不存在！");
				return "/login2.jsp";
			} else {
				if (password.equals(userService.getPasswordByUsername(username))) {
					model.addAttribute("msg", "登录成功！");
					return "/WEB-INF/main.jsp";
				} else {
					model.addAttribute("msg", "密码错误！");
					return "/login2.jsp";
				}
			}
		}*/
		
		
		@RequestMapping(value = "login", method = RequestMethod.POST)
		public String login(@RequestParam("username") String username,
				@RequestParam("password") String password,ModelMap model) {
			if (!userService.isUserExist(username)) {
				model.addAttribute("msg", "用户名不存在！");
				return "/login2.jsp";
			} else {
				if (password.equals(userService.getPasswordByUsername(username))) {
					if(username.equals("aaa")){
						model.addAttribute("msg", "管理员登录！");
//						return "/main.jsp";
						return "forward:shouye";
					}else{
						model.addAttribute("msg", "登录成功！");
						return "/forward:userShouYe";
					}
					
				} else {
					model.addAttribute("msg", "密码错误！");
					return "/login2.jsp";
				}
			}
		}
	 
		//关于用户注册的表单提交
		@RequestMapping(value = "register", method = RequestMethod.POST)
		public String register(@RequestParam("username") String username,
				@RequestParam("password") String password,@RequestParam("email") String email, ModelMap model) {
			if (userService.isUserExist(username)) {
				
				model.addAttribute("msg", "用户名已存在！");
				return "/register2.jsp";
			} else {
				userService.addUser(username, password,email);
				model.addAttribute("msg", "注册成功！");
				return "/login2.jsp";
			}
//			return "index";
		}
	 
		//关于修改密码的表单提交
		@RequestMapping(value = "updatePassword", method = RequestMethod.POST)
		public String updatePassword(@RequestParam("username") String username,
				@RequestParam("password") String password,
				@RequestParam("newpassword") String newpassword,
				@RequestParam("email") String email,ModelMap model) {
			if (!userService.isUserExist(username)) {
				model.addAttribute("msg", "用户名不存在！");
				return "/update2.jsp";
			} else {
				if (password.equals(userService.getPasswordByUsername(username))) {
					if(username.equals("aaa")){
						userService.modifyPasswordByUsernameAndEmail(username, newpassword,email);
						model.addAttribute("msg", "个人信息修改成功！");
//						return "/main.jsp";
						return "forward:shouye";
					}else{
						userService.modifyPasswordByUsernameAndEmail(username, newpassword,email);
						model.addAttribute("msg", "个人信息修改成功！");
						return "/main2.jsp";
					}
					
				} else {
					model.addAttribute("msg", "密码错误！");
					return "/update2.jsp";
				}
			}
			
		}
	

}
